package com.kelvin.paopao.module.chat.friend;

import com.kelvin.paopao.api.RetrofitService;
import com.kelvin.paopao.api.responseBean.CommonListResponse;
import com.kelvin.paopao.api.responseBean.DatePersonModel;
import com.kelvin.paopao.module.base.IBasePresenter;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

/**
 * Created by Administrator on 2017/5/10.
 */

public class FriendPresenter implements IBasePresenter {
    private FriendActivity mVIew;

    public FriendPresenter(FriendActivity view) {
        this.mVIew = view;
    }


    @Override
    public void getData(boolean isRefresh) {
        RetrofitService.getFriend().doOnSubscribe(new Consumer<Disposable>() {
            @Override
            public void accept(@NonNull Disposable disposable) throws Exception {
                mVIew.showLoading();
            }
        }).subscribeOn(AndroidSchedulers.mainThread()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<CommonListResponse<DatePersonModel>>() {
                    @Override
                    public void accept(@NonNull CommonListResponse<DatePersonModel> commonListResponse) throws Exception {
                        mVIew.hideLoading();

                        if (commonListResponse.getResult().size() == 0) {
                            mVIew.showNoData();
                        } else {
                            mVIew.loadData(commonListResponse.getResult());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@NonNull Throwable throwable) throws Exception {
                        mVIew.hideLoading();
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        mVIew.hideLoading();
                    }
                });
    }

    @Override
    public void getMoreData() {

    }
}
